﻿using System;
using System.Reflection;
using System.Configuration;
using JKF.DataModule.IDAL;
namespace JKF.DataModule.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="JKF.Entities.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess_Sales
    {
        //private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];可以在配置文件中读取
        private static readonly string AssemblyPath = "JKF.DataModule";//动态库文件
        private static readonly string NameSpase = "JKF.DataModule.SqlServerDAL";//命名空间
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch (Exception ex)
                {
                    throw new Exception("反射创建数据处出错！", ex);
                }
            }
            return objType;
        }
        /// <summary>
        /// 创建dalSALES_CONSUME数据层接口
        /// </summary>
        public static JKF.DataModule.IDAL.IdalSALES_CONSUME CreatedalSALES_CONSUME()
        {

            string ClassNamespace = NameSpase + ".dalSALES_CONSUME";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (JKF.DataModule.IDAL.IdalSALES_CONSUME)objType;
        }

        /// <summary>
        /// 创建dalSALES_CONTRACT数据层接口
        /// </summary>
        public static JKF.DataModule.IDAL.IdalSALES_CONTRACT CreatedalSALES_CONTRACT()
        {

            string ClassNamespace = NameSpase + ".dalSALES_CONTRACT";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (JKF.DataModule.IDAL.IdalSALES_CONTRACT)objType;
        }


        /// <summary>
        /// 创建dalSALES_NUTRITION数据层接口
        /// </summary>
        public static JKF.DataModule.IDAL.IdalSALES_NUTRITION CreatedalSALES_NUTRITION()
        {

            string ClassNamespace = NameSpase + ".dalSALES_NUTRITION";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (JKF.DataModule.IDAL.IdalSALES_NUTRITION)objType;
        }

    }
}